package com.manxi.mapper;

import com.github.pagehelper.Page;
import com.manxi.annotation.AutoFill;
import com.manxi.dto.CategoryPageQueryDTO;
import com.manxi.entity.Category;
import com.manxi.enumeration.OperationType;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface CategoryMapper {

    /**
     * 分类分页查询
     *
     * @param categoryPageQueryDTO
     * @return
     */
    @Select("<script> " +
            "select * from category " +
            "where 1 = 1 " +
            "<if test='name != null'> AND name like CONCAT('%',#{name},'%')</if> " +
            "<if test='type != null'> AND type = #{type}</if> " +
            "order by sort asc , create_time desc " +
            "</script>")
    Page<Category> pageQuery(CategoryPageQueryDTO categoryPageQueryDTO);

    /**
     * 新增分类
     *
     * @param category
     * @return
     */
    @Insert("insert into category (name,type,sort,status,create_time,update_time,create_user,update_user) values(#{name},#{type},#{sort},#{status},#{createTime},#{updateTime},#{createUser},#{updateUser})")
    @AutoFill(value = OperationType.INSERT)
    void insert(Category category);

    /**
     * 删除分类
     *
     * @param id
     * @return
     */
    @Delete("delete from category where id = #{id}")
    void delete(Long id);

    /**
     * 启售禁售分类(修改分类信息)
     *
     * @param category
     */
    @Update("<script> " +
            "update category set " +
            "<if test='type != null'> type = #{type}, </if> " +
            "<if test='name != null'> name = #{name}, </if> " +
            "<if test='sort != null'> sort = #{sort}, </if> " +
            "<if test='updateTime != null'> update_time = #{updateTime}, </if> " +
            "<if test='updateUser != null'> update_user = #{updateUser} </if> " +
            "<if test='status != null'> status = #{status} </if> " +
            "where id = #{id} " +
            "</script>")
    @AutoFill(value = OperationType.UPDATE)
    void update(Category category);

    /**
     * 根据类型查询分类
     *
     * @param type
     * @return
     */
    @Select("<script>" +
            "select * from category " +
            "where status = 1 " +
            "<if test='type != null'>AND type = #{type} </if>" +
            "order by sort asc,create_time desc " +
            "</script>")
    List<Category> list(Integer type);
}
